﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Contract.Sys;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Dto.Sys;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Contracts.Model.Sys;
using CRM.Admin.Services.Service.Customer;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.Helper;
using KingsSharp.Web.UI;
using System.IO;
using NPOI.XSSF.UserModel;
using KingsSharp.Common.Data;
using CRM.Admin.Contracts.Model.Map;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class MarketingController : Controller
    {
        // GET: Customer/Marketing      
        public const string CUSTOMER_FILEPATH = "BaseCustomerfiles//";
        public IAreaRequestService AreaRequstService { get;set;}
        public IIndustryRequstService IndustryRequstService { get; set; }

        public IUserRequstService UserRequstService { get; set; }

        public ISupplyService SupplyService { get; set; }
        public INetworkService NetWorkService { get; set; }
        public ISaleService SaleService { get; set; }
        public IAreaProfilesService AreaProfilesService { get; set; }
        #region 视图功能
        /// <summary>
        /// 需求情况按地区
        /// </summary>
        /// <returns></returns>
        public ActionResult AreaRequst()
        {
            return View();
        }
        /// <summary>
        /// 需求情况按行业
        /// </summary>
        /// <returns></returns>
        public ActionResult IndustryRequst()
        {
            return View();
        }
        /// <summary>
        /// 需求情况按终端用户
        /// </summary>
        /// <returns></returns>
        public ActionResult UserRequst()
        {
            return View();
        }
        /// <summary>
        /// 供给情况
        /// </summary>
        /// <returns></returns>
        public ActionResult Supply()
        {
            return View();
        }
        /// <summary>
        /// 网点简介
        /// </summary>
        /// <returns></returns>
        public ActionResult Network()
        {
            return View();
        }
        /// <summary>
        /// 修改网点简介
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult EditNetwork(Guid Id)
        {
            var entity = NetWorkService.Networks.FirstOrDefault(a => a.Id == Id).MapTo<NetworkDto>();
            return View(entity);
        }
        /// <summary>
        /// 编辑网点简介
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateNetwork(Guid Id,string netWorkName,string profiles)
        {
            var entity = NetWorkService.Networks.FirstOrDefault(a => a.Id == Id).MapTo<NetworkDto>();
            entity.NetworkName = netWorkName;
            entity.Profiles = profiles;
            var result = NetWorkService.Update(entity);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "简介更新成功";
            }
            return Json(new AjaxResult(msg, msgType));

        }
        /// <summary>
        /// 销售流向
        /// </summary>
        /// <returns></returns>
        public ActionResult Sale()
        {
            return View();
        }
        /// <summary>
        /// 地图视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Map()
        {
            return View();
        }
        public ActionResult AreaProfiles()
        {
            return View();
        }
        /// <summary>
        /// 修改简介
        /// </summary>
        /// <returns></returns>
        public ActionResult EditProfiles(Guid Id)
        {
            var entity = AreaProfilesService.AreaProfiles.FirstOrDefault(a=>a.Id==Id).MapTo<AreaProfilesDto>();
            return View(entity);
        }
        /// <summary>
        /// 编辑简介
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateProfiles(Guid Id,string profiles)
        {
            var entity = AreaProfilesService.AreaProfiles.FirstOrDefault(a=>a.Id==Id);
            entity.Profiles = profiles;
            var result = AreaProfilesService.Update(entity.MapTo<AreaProfilesDto>());
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "简介更新成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        #endregion
        #region 数据功能
        #region 需求情况按地区
        /// <summary>
        ///导入Excel需求情况-按地区
        /// </summary>
        /// <returns></returns>
        public ActionResult AreaRequestExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<AreaRequstDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new AreaRequstDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Year = Convert.ToInt32(row.GetCell(0).ToString());
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Area =row.GetCell(1).ToString();
                        }
                        if (row.GetCell(2) != null)
                        {
                            model.Province = row.GetCell(2).ToString();
                        }
                        if (row.GetCell(3) != null)
                        {
                            model.City = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4) != null)
                        {
                            model.ProdName = row.GetCell(4).ToString();
                        }
                        if (row.GetCell(5) != null)
                        {
                            model.ReqWt =Convert.ToDecimal( row.GetCell(5).ToString());
                        }
                    
                        list.Add(model);

                    }
                    var res = AreaRequstService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "行数据格式有误";
            }
            return Json(result);
        }
        /// <summary>
        /// 需求情况-按地区数据功能
        /// </summary>
        /// <returns></returns>
        public ActionResult AreaRequestGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<AreaRequstInfo, bool>> predicate = FilterHelper.GetExpression<AreaRequstInfo>(request.FilterGroup);
            int total;
            var data = AreaRequstService.AreaRequsts
                .Where<AreaRequstInfo,Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto=> new {
                        dto.Year,
                        dto.Area,
                        dto.Province,
                        dto.City,
                        dto.ProdName,
                        dto.ReqWt
                }).ToList();
            return Json(new GridData<object>(data,total),JsonRequestBehavior.AllowGet);
               
        }
        #endregion
        #region 需求情况-按行业
        /// <summary>
        /// 导入Excel需求情况-按行业
        /// </summary>
        /// <returns></returns>
        public ActionResult IndustryRequstExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<IndustryRequstDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new IndustryRequstDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Year = Convert.ToInt32(row.GetCell(0).ToString());
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Area = row.GetCell(1).ToString();
                        }
                        if (row.GetCell(2) != null)
                        {
                            model.Province = row.GetCell(2).ToString();
                        }
                        if (row.GetCell(3) != null)
                        {
                            model.ProdName = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4) != null)
                        {
                            model.Industry = row.GetCell(4).ToString();
                        }
                        if (row.GetCell(5) != null)
                        {
                            model.ReqWt = Convert.ToDecimal(row.GetCell(5).ToString());
                        }

                        list.Add(model);

                    }
                    var res = IndustryRequstService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "行数据格式有误";
            }
            return Json(result);
        }
       
        /// <summary>
        /// 需求情况-按行业数据功能
        /// </summary>
        /// <returns></returns>
        public ActionResult IndustryRequstGridata()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<IndustryRequstInfo, bool>> predicate = FilterHelper.GetExpression<IndustryRequstInfo>(request.FilterGroup);
            int total;
            var data = IndustryRequstService.IndustryRequsts
                .Where<IndustryRequstInfo, Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new
                {
                    dto.Year,
                    dto.Area,
                    dto.Province,
                    dto.Industry,
                    dto.ProdName,
                    dto.ReqWt
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 需求情况-按终端用户
        /// <summary>
        /// 导入Excel需求情况-按终端用户
        /// </summary>
        /// <returns></returns>
        public ActionResult UserRequstExcelInert()
        {
            UploaderResult result = null;
            int rowmsg=0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<UserRequstDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                  
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        var row = sheet.GetRow(i);
                        rowmsg = i;
                        var model = new UserRequstDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Year = Convert.ToInt32(row.GetCell(0).ToString());
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Area = row.GetCell(1).ToString();
                        }
                        if (row.GetCell(2) != null)
                        {
                            model.Province = row.GetCell(2).ToString();
                        }
                        if (row.GetCell(3)!=null)
                        {
                            model.ProvinceArea = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4) != null)
                        {
                            model.City = row.GetCell(4).ToString();
                        }
                        if (row.GetCell(5) != null)
                        {
                            model.ProdName = row.GetCell(5).ToString();
                        }
                        if (row.GetCell(6) != null)
                        {
                            model.Industry = row.GetCell(6).ToString();
                        }
                        if (row.GetCell(7) != null)
                        {
                            model.FinUserName = row.GetCell(7).ToString();
                        }
                        if (row.GetCell(8) != null)
                        {
                            model.ReqWt = Convert.ToDecimal(row.GetCell(8).ToString());
                        }
                        if (row.GetCell(9)!=null)
                        {
                            model.FinUserProfIles = row.GetCell(9).ToString();
                        }

                        list.Add(model);

                    }
                    var res = UserRequstService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第"+(rowmsg+1)+"数据格式有误";
            }
            return Json(result);
        }
        /// <summary>
        /// 需求情况-按终端用户数据
        /// </summary>
        /// <returns></returns>
        public ActionResult UserRequstGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<UserRequstInfo, bool>> predicate = FilterHelper.GetExpression<UserRequstInfo>(request.FilterGroup);
            int total;
            var data = UserRequstService.UserRequsts
                .Where<UserRequstInfo, Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new {
                    dto.Year,
                    dto.Area,
                    dto.Province,
                    dto.ProvinceArea,
                    dto.City,
                    dto.FinUserName,
                    dto.Industry,
                    dto.ProdName,
                    dto.ReqWt,
                    FinUserProfIles = string.IsNullOrEmpty(dto.FinUserProfIles)?"":
                    dto.FinUserProfIles.Length<16?dto.FinUserProfIles:dto.FinUserProfIles.Substring(0,15)+"..."
                }).ToList();
            return Json(new GridData<object>(data,total),JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 供给情况
        /// <summary>
        /// 导入Excel供给情况
        /// </summary>
        /// <returns></returns>
        public ActionResult SupplyExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<SupplyDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new SupplyDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Year = Convert.ToInt32(row.GetCell(0).ToString());
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Area = row.GetCell(1).ToString();
                        }
                        if (row.GetCell(2) != null)
                        {
                            model.Province = row.GetCell(2).ToString();
                        }
                        if (row.GetCell(3) != null)
                        {
                            model.SteelFactory = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4) != null)
                        {
                            model.SteelFactoryArea = row.GetCell(4).ToString();
                        }
                        if (row.GetCell(5) != null)
                        {
                            model.ProdName = row.GetCell(5).ToString();
                        }
                        if (row.GetCell(6) != null)
                        {
                        object a = row.GetCell(6);
                            model.SupWt =Convert.ToDecimal(row.GetCell(6).ToString());
                        }
                        if (row.GetCell(7) != null)
                        {
                            model.ProportIon =Convert.ToDecimal( row.GetCell(7).ToString());
                        }
                       
                        list.Add(model);

                    }
                    var res = SupplyService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "数据格式有误";
            }
            return Json(result);
        }
        public ActionResult SupplyGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<SupplyInfo, bool>> predicate = FilterHelper.GetExpression<SupplyInfo>(request.FilterGroup);
            int total;
            var data = SupplyService.Supplys
                .Where<SupplyInfo,Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new {
                    dto.Year,
                    dto.Area,
                    dto.Province,
                    dto.SteelFactory,
                    dto.SteelFactoryArea,
                    dto.ProdName,
                    dto.SupWt,
                    dto.ProportIon
                }).ToList();
            return Json(new GridData<object>(data,total),JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 网店简介
        /// <summary>
        /// 导入Excel网店简介
        /// </summary>
        /// <returns></returns>
        public ActionResult NetworkExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<NetworkDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new NetworkDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.NetworkArea = row.GetCell(0).ToString();
                        }                      
                        if (row.GetCell(1)!=null)
                        {
                            model.Longitude = Convert.ToDecimal(row.GetCell(1).ToString()); 
                        }
                        if (row.GetCell(2)!=null)
                        {
                            model.Latitude = Convert.ToDecimal(row.GetCell(2).ToString());
                        }
                        if (row.GetCell(3)!=null)
                        {
                            model.Profiles = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4)!=null)
                        {
                            model.NetworkName = row.GetCell(4).ToString();
                        }
                        list.Add(model);

                    }
                    var res = NetWorkService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "数据格式有误"; ;
            }
            return Json(result);
        }
        public ActionResult NetWorkGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<NetworkInfo, bool>> predicate = FilterHelper.GetExpression<NetworkInfo>(request.FilterGroup);
            int total;
            var data = NetWorkService.Networks
                .Where<NetworkInfo,Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new
                {
                    dto.Id,
                    dto.NetworkArea,
                    dto.Profiles,
                    dto.NetworkName
                }).ToList();
            return Json(new GridData<object>(data,total),JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 简介
        /// <summary>
        /// 导入简介Excel
        /// </summary>
        /// <returns></returns>
        public ActionResult AreaProfilesExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<AreaProfilesDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new AreaProfilesDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Area = row.GetCell(0).ToString();
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Profiles = row.GetCell(1).ToString();
                        }
                        list.Add(model);

                    }
                    var res = AreaProfilesService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "数据格式有误"; ;
            }
            return Json(result);
        }
        public ActionResult AreaProfilesGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<AreaProfilesInfo, bool>> predicate = FilterHelper.GetExpression<AreaProfilesInfo>(request.FilterGroup);
            int total;
            var data = AreaProfilesService.AreaProfiles
                .Where<AreaProfilesInfo, Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new
                {
                    dto.Id,
                    dto.Area,
                    dto.Profiles
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #endregion
        #region 销售流向
        /// <summary>
        /// 导入Excel销售流向
        /// </summary>
        /// <returns></returns>
        public ActionResult SaleExcelInert()
        {
            UploaderResult result = null;
            int rowmsg = 0;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
                var list = new List<SaleDto>();
                var filePath = result.Path;

                if (filePath == null) return Json("");

                var savepath = "//" + result.Path;
                var stream = new FileStream(Server.MapPath(savepath), FileMode.Open, FileAccess.Read);

                using (stream)
                {
                    var excel = new XSSFWorkbook(stream);
                    var sheet = excel.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        rowmsg = i;
                        var row = sheet.GetRow(i);
                        var model = new SaleDto();
                        model.CreateById = AppVariable.UserData.UserId;

                        if (row.GetCell(0) != null)
                        {
                            model.Year = Convert.ToInt32(row.GetCell(0).ToString());
                        }
                        if (row.GetCell(1) != null)
                        {
                            model.Area = row.GetCell(1).ToString();
                        }
                        if (row.GetCell(2) != null)
                        {
                            model.Province = row.GetCell(2).ToString();
                        }
                        if (row.GetCell(3) != null)
                        {
                            model.ProdName = row.GetCell(3).ToString();
                        }
                        if (row.GetCell(4) != null)
                        {
                            model.Industry = row.GetCell(4).ToString();
                        }
                        if (row.GetCell(5) != null)
                        {
                            model.SaleWt = Convert.ToDecimal(row.GetCell(5).ToString());
                        }
                        if (row.GetCell(6)!=null)
                        {
                            model.ProportIon = Convert.ToDecimal(row.GetCell(6).ToString());
                        }
                        list.Add(model);

                    }
                    var res = SaleService.Insert(list.ToArray());

                    if (res.ResultType == OperationResultType.Error)
                    {
                        result.Message = res.Message;
                    }

                    if (res.ResultType == OperationResultType.Success)
                    {
                        result.Message = res.Message;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = "第" + (rowmsg + 1) + "行数据格式有误";
            }
            return Json(result);
        }

        /// <summary>
        /// 销售流向数据功能
        /// </summary>
        /// <returns></returns>
        public ActionResult SaleGridata()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<SaleInfo, bool>> predicate = FilterHelper.GetExpression<SaleInfo>(request.FilterGroup);
            int total;
            var data = SaleService.Sales
                .Where<SaleInfo, Guid>(predicate, request.PageCondition, out total)
                .Where(a => a.IsDeleted == false)
                .Select(dto => new
                {
                    dto.Year,
                    dto.Area,
                    dto.Province,
                    dto.Industry,
                    dto.ProdName,
                    dto.SaleWt,
                    dto.ProportIon
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}